﻿using System.Collections.Generic;
/*
 * Created by SharpDevelop.
 * User: CeCi
 * Date: 25/04/2014
 * Time: 07:23 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System.Data;
using System;

namespace Gestión_UTN.blimpl
{
    /// <summary>
    /// Description of notablimpl.
    /// </summary>
    public class notablimpl : bl.notabl
    {
        private static da.notada createNota = factories.dafactory.createNota();
        private static bl.alumnobl createAlumno = factories.blfactory.createAlumno();
        private static bl.fechaExamenbl createFechaExamen = factories.blfactory.createFechaExamen();

        public notablimpl()
        {
        }

        public bool SaveNota(Gestión_UTN.bo.Notabo not)
        {
            bool ingresada = false;

            try
            {
                ingresada = (null != not)
                    ? createNota.SaveNota(not.Alumno.Dni,
                                          not.FechaExamen.Fe_id, not.Valor)
                    : false;
            }
            catch (exceptions.daexception ex)
            {
                ingresada = false;
                throw new exceptions.blexception(ex.Message);
            }
            catch (Exception ex)
            {
                ingresada = false;
                throw new exceptions.blexception(ex.Message);
            }

            return ingresada;
        }

        public bool DeleteNota(int alu_dni, int fe_id)
        {
            bool eliminada = false;

            try
            {
                eliminada = (0 != alu_dni && 0 != fe_id)
                    ? createNota.DeleteNota(alu_dni, fe_id)
                    : false;
            }
            catch (exceptions.daexception ex)
            {
                eliminada = false;
                throw new exceptions.blexception(ex.Message);
            }
            catch (Exception ex)
            {
                eliminada = false;
                throw new exceptions.blexception(ex.Message);
            }

            return eliminada;
        }

        public bool ModifyNota(Gestión_UTN.bo.Notabo not)
        {
            bool modificada = false;

            try
            {
                modificada = (null != not)
                    ? createNota.ModifyNota(not.Alumno.Dni, not.FechaExamen.Fe_id, not.Valor)
                    : false;
            }
            catch (exceptions.daexception ex)
            {
                modificada = false;
                throw new exceptions.blexception(ex.Message);
            }
            catch (Exception ex)
            {
                modificada = false;
                throw new exceptions.blexception(ex.Message);
            }

            return modificada;
        }

        public System.Data.DataTable FindAllNotas()
        {
            DataTable tablaNotas = null;

            try
            {
                tablaNotas = createNota.FindAllNotas();
            }
            catch (exceptions.daexception ex)
            {
                tablaNotas = null;
                throw new exceptions.blexception(ex.Message);
            }
            catch (Exception ex)
            {
                tablaNotas = null;
                throw new exceptions.blexception(ex.Message);
            }

            return tablaNotas;
        }

        public System.Collections.Generic.List<Gestión_UTN.bo.Notabo> FindNotaByDni(int alu_dni)
        {
            List<bo.Notabo> notas = null;

            try
            {
                List<Entities.Nota> nots = (0 != alu_dni) ? createNota.FindNotaByDni(alu_dni) : null;

                if (null != nots)
                {
                    notas = new List<Gestión_UTN.bo.Notabo>();

                    foreach (var n in nots)
                    {
                        bo.Alumnobo alu = createAlumno.FindAlumnoByDNI(n.Alu_dni);

                        bo.FechaExamenbo fec = createFechaExamen.FindExamenbyId(n.Fe_id);

                        bo.Notabo not = new bo.Notabo(alu, fec, n.Valor);

                        notas.Add(not);

                        not = null;

                        alu = null;

                        fec = null;
                    }
                }
            }
            catch (exceptions.daexception ex)
            {
                notas = null;
                throw new exceptions.blexception(ex.Message);
            }
            catch (Exception ex)
            {
                notas = null;
                throw new exceptions.blexception(ex.Message);
            }

            return notas;
        }

        public System.Collections.Generic.List<Gestión_UTN.bo.Notabo> FindNotaByFecha(int fe_id)
        {
            List<bo.Notabo> notas = null;

            try
            {
                List<Entities.Nota> nots = (0 != fe_id) ? createNota.FindNotaByFecha(fe_id) : null;

                if (null != nots)
                {
                    notas = new List<Gestión_UTN.bo.Notabo>();

                    foreach (var n in nots)
                    {
                        bo.Alumnobo alu = createAlumno.FindAlumnoByDNI(n.Alu_dni);

                        bo.FechaExamenbo fec = createFechaExamen.FindExamenbyId(n.Fe_id);

                        bo.Notabo not = new bo.Notabo(alu, fec, n.Valor);

                        notas.Add(not);

                        not = null;

                        alu = null;

                        fec = null;
                    }
                }
            }
            catch (exceptions.daexception ex)
            {
                notas = null;
                throw new exceptions.blexception(ex.Message);
            }
            catch (Exception ex)
            {
                notas = null;
                throw new exceptions.blexception(ex.Message);
            }

            return notas;
        }
    }
}
